var express = require('express');
var router = express.Router();
var path = require('path');
var v = new Date().getTime();
var qiniu = require('../tools/qiniu_token');
var orderType = {'1': '待支付订单', '2': '待提货订单', '3': '已完成订单', '4': '已取消订单'};
router.get('/getToken', function (req, res, next) {
    var data = {token: qiniu.token, nanotime: qiniu.nt};
    res.json(data);
});

//自动路由  在指定路由顺序之下
router.get('/', function (req, res, next) {
  res.redirect('/index.html');
});
router.get('/order/order_list.html', function (req, res, next) {
  var title = orderType[req.param('ot')] || orderType['1']
  res.render('order/order_list.html', {title: title, v: v});
});
router.get('*', function (req, res, next) {
  var _reqUrl = req.path.substring(1);
  var viewsFolder = path.resolve(__dirname, '../views/',_reqUrl);
  if (global.__pagesArr.indexOf(viewsFolder) !== -1) {//判断请求地址是否存在
      res.render(_reqUrl, {title: 'city bar', v: v});
  } else {
      next();//如果视图目录无匹配视图，向下传递继续处理
  }
});

module.exports = router;